package com.magesti.beans;

import java.util.List;

import com.magesti.ressource.Global;

public class Phase implements Comparable<Phase>
{
	private int id = -1;
	private String nom = null;
	private String description = null;
	private String etape = null;
	private String dateDebutPrevi = null;
	private String dateFinPrevi = null;
	private String dateDebutAvancement = null;
	private String dateFinAvancement = null;
	private String nature = null;// {PI,Travaux,autre};
	private String budgetPrevi = null;
	private String observations = null;
	private String couleur = Global.VALEUR_COULEUR_DEFAUT;
	private int ordre = -1; // defini l'ordre dans le deroulement des phases. 
	
	private List<OrdreDeService> listeOS;

	public List<OrdreDeService> getListeOS()
	{
		return listeOS;
	}

	public void putOS(OrdreDeService os)
	{
		listeOS.add(os);
	}

	/*
	 * public void getSpecificOS() {
	 * 
	 * }
	 */
	
	public boolean getIsNotCheckAvancement(){
		return (dateFinAvancement==null || dateFinAvancement.isEmpty()) && (dateDebutAvancement==null || dateDebutAvancement.isEmpty());
	}

	public int getId()
	{
		return id;
	}

	public void setId(int iD)
	{
		id = iD;
	}

	public String getNom()
	{
		return nom;
	}

	public void setNom(String nom)
	{
		this.nom = nom;
	}

	public String getDateDebutPrevi()
	{
		return dateDebutPrevi;
	}

	public void setDateDebutPrevi(String dateDebutPrevi)
	{
		this.dateDebutPrevi = psqlTransformer(dateDebutPrevi);
	}

	public String getDateFinPrevi()
	{
		return dateFinPrevi;
	}

	public void setDateFinPrevi(String dateFinPrevi)
	{
		this.dateFinPrevi = psqlTransformer(dateFinPrevi);
	}

	public String getDateDebutAvancement()
	{
		return dateDebutAvancement;
	}

	public void setDateDebutAvancement(String dateDebutAvancement)
	{
		this.dateDebutAvancement = psqlTransformer(dateDebutAvancement);
	}

	public String getDateFinAvancement()
	{
		return dateFinAvancement;
	}

	public void setDateFinAvancement(String dateFinAvancement)
	{
		this.dateFinAvancement = psqlTransformer(dateFinAvancement);
	}

	public String getNature()
	{
		return nature;
	}

	public void setNature(String nature)
	{
		this.nature = nature;
	}

	public String getBudgetPrevi()
	{
		return budgetPrevi;
	}

	public void setBudgetPrevi(String budgetPrevi)
	{
		this.budgetPrevi = budgetPrevi;
	}

	public String getObservations()
	{
		return observations;
	}

	public void setObservations(String observations)
	{
		this.observations = observations;
	}

	public String getDescription()
	{
		return description;
	}

	public void setDescription(String description)
	{
		this.description = description;
	}

	public int getOrdre()
	{
		return ordre;
	}

	public void setOrdre(int ordre)
	{
		this.ordre = ordre;
	}

	@Override
	public int compareTo(Phase p)
	{
		return p.getOrdre() < this.ordre ? 1 : -1;
	}

	public String getEtape()
	{
		return etape;
	}

	public void setEtape(String etape)
	{
		this.etape = etape;
	}

	public String getCouleur() {
		return couleur;
	}

	public void setCouleur(String couleur) {
		this.couleur = couleur;
	}

	//TODO Supprimer cette methode une fois sous oracle.
	private String psqlTransformer(String s){
		String regex = "(\\d{4}).(\\d{2}).(\\d{2})";

		if(!(s==null || s.trim().isEmpty()) && s.matches(regex)){
			return s.replaceAll(regex, "$3/$2/$1");
		}
		return s;
	}

	public String getDateDebutPreviJS() {
		return toJs(dateDebutPrevi);
	}

	public String getDateFinPreviJS() {
		return toJs(dateFinPrevi);
	}

	public String getDateDebutAvancementJS() {
		return toJs(dateDebutAvancement);
	}

	public String getDateFinAvancementJS() {
		return toJs(dateFinAvancement);
	}
	
	private String toJs(String s){
		String regex = "(\\d{2}).(\\d{2}).(\\d{4})";

		if(!(s==null || s.trim().isEmpty()) && s.matches(regex)){
			return s.replaceAll(regex, "$2/$1/$3");
		}
		return "";
	}
	
	public String toString(){
		String out ="";
		
		out+="Nom: "+nom +" ("+ordre+") ddp: "+dateDebutPrevi+" dfp:"+dateFinPrevi;
		if(dateDebutAvancement != null)
			out+=" dda"+dateDebutAvancement+" dfa: "+dateFinAvancement;
		else
			out+=" (pas de dda ni de dfa)";
		
		
		
		return out;
	}
}
